package com.bobi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bobi.entity.Promotion;
import com.bobi.entity.Staff;
import com.bobi.enums.ResultCode;
import com.bobi.service.StaffService;
import com.bobi.utils.PageUtil;
import com.bobi.utils.ResultUtil;
import com.bobi.utils.Token;
import com.bobi.vo.PageVO;
import com.bobi.vo.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @author wenyuan
 * @version 1.0
 * @date 2023/7/4
 * @since 8
 */
@RestController
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;


    @PostMapping("/login")
    public ResultMessage<Token> login(@NotNull(message = "用户名不能为空") @RequestParam String username,
                                      @NotNull(message = "密码不能为空") @RequestParam String password) {
            return ResultUtil.data(staffService.login(username, password));
    }


    /**
     * 分页查询所有信息
     * @param pageVO 分页对象
     * @return 返回分页对象
     */
    @GetMapping(value=  "/page")
    public ResultMessage<Page<Staff>> page(Staff staff, PageVO pageVO){
        if(staff == null){
            return ResultUtil.error(ResultCode.PROMOTION_SEARCH_ERROR);
        }
        QueryWrapper<Staff> queryWrapper = staff.initWrapper(staff);
        Page<Staff> page = PageUtil.initPage(pageVO);
        Page<Staff> list;
        if(queryWrapper == null){
            list = staffService.page(page);
        }else{
            list = staffService.page(page, queryWrapper);
        }
        return ResultUtil.data(list);
    }

    /**
     * 添加员工
     * @param staff 促销员工对象
     * @return 返回的标志
     */
    @PostMapping
    public ResultMessage<Object> addStaff(@RequestBody Staff staff){
        // 如果删除成功
        if(staffService.save(staff)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultCode.PROMOTION_ADD_ERROR);
    }

    /**
     * 更新员工
     * @param staff 员工对象
     * @return 返回对象
     */
    @PutMapping("/{id}")
    public ResultMessage<Object> updateActivity(@PathVariable String id,@RequestBody Staff staff){
        // 构造查询条件包装
        UpdateWrapper<Staff> updateWrapper = new UpdateWrapper<Staff>().eq("id", id);
        // 如果更新成功
        if(staffService.update(staff, updateWrapper)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultCode.PROMOTION_UPDATE_ERROR);
    }

    /**
     * 删除员工(逻辑删除)
     * @param id 员工id
     * @return 结果消息
     */
    @DeleteMapping("/{id}")
    public ResultMessage<Object>  deleteActivity(@PathVariable("id") String id){
        if(id == null){
            return ResultUtil.error(ResultCode.PROMOTION_DELETE_ERROR);
        }
        staffService.removeById(id);
        return ResultUtil.success();
    }

    @DeleteMapping("/all")
    public ResultMessage<Object>  deleteBatchActivity(@RequestParam List<String> ids){
        if(ids == null){
            return ResultUtil.error(400014,"删除全部失败！！");
        }
        staffService.removeBatchByIds(ids);
        return ResultUtil.success();
    }
}
